(function() {

	"use stricts";

	var tools = function() {

		var self = {};

		self.md5ByArray = function(list) {
			var arr = [];
			for (var i = 0; i < list.length; i++) {
				var obj = list[i];
				arr.push(obj.key + "=" + obj.value);
			}
			var str = arr.join("&");
			return md5(str);
		};

		self.createSign = function(request, fields) {

			var arr = [];
			for (var i = 0; i < fields.length; i++) {
				var key = fields[i];
				var val = request[key];
				if(val != "" && val != undefined) {
					arr.push(key + "=" + val);
				}
			}

			var val = arr.join("&");
			console.log(val);
			return md5(val);
		};

		self.mapToUrl = function(map, encode) {
			var params = [];
			var array = map.toArray();
			
			for (var i = 0; i < array.length; i++) {
				var data = array[i];
				if(data.value != '' && data.value != null) {
					if(encode) {
						params.push(data.key + "=" + encodeURIComponent(data.value));
					} else {
						params.push(data.key + "=" + data.value);
					}
				}
			}
			return params.join("&");
		};

		self.objectToUrl = function(obj) {
			var arr = [];
			for(key in obj) {
				var val = obj[key] || "";
				if(val != "") {
					arr.push(key + "=" + val);
				}
			}
			arr.sort();
			return arr.join("&");
		};

		self.buildParamUrl = function(param) {
			var arr = [];
			for(key in param) {
				var val = param[key] || "";
				arr.push(key + "=" + encodeURIComponent(val));
			}
			return arr.join("&");
		};

		self.jumpByParam = function(url, param) {
			var src = url + "?" + self.buildParamUrl(param);
			location.href = src;
		};

		self.openByParam = function(url, param, _cb) {
			var src = url + "?" + self.buildParamUrl(param);
			window.open(src);
			if (_cb != null) { 
				_cb(src);
			}
		};

		self.buildUrl = function(url, param) {
			var src;
			// if(typeof param == "string")
			if(param == null) {
				src = url;
			} else if(typeof param == "string") {
				src = url + "?" + param;
			} else if(typeof param == "object") {
				// src = url + "?" + $.param(param);
				src = url + "?" + self.buildParamUrl(param);
			}
			return src;
		};

		// test.com?query
		self.open = function(url, param, _cb) {
			var src = self.buildUrl(url, param);
			window.open(src);
			if (_cb != null) { 
				_cb(src);
			}
		};

		// 简易get
		self._get = function(url) {
			$.get(url, null, function(data) {
				console.log(data);
			});
		};

		self.xmlToObj = function(xml) {
			return $.xml2json(xml);
		};

		self.getParam = function(paramUrl) {
			var paramStrs = paramUrl.split("&");
			var param = {};
			for(var index in paramStrs) {
                var paramStr = paramStrs[index];
                if(paramStr == "") {
                    continue;
                }
				var paramKeyValue = paramStr.split("=");
				var key = paramKeyValue[0];
				var value = paramKeyValue[1]; 
				param[key] = decodeURIComponent(value);
			}
			return param;
		}

		self.getParamByUrl = function() {
			var url = location.href;
			if(url.indexOf("?") == -1) {
				return {};
			}
			var paramUrl = url.split("?")[1];
			var paramStrs = paramUrl.split("&");
			var param = {};
			for(var index in paramStrs) {
				var paramStr = paramStrs[index];
				var paramKeyValue = paramStr.split("=");
				var key = paramKeyValue[0];
				var value = paramKeyValue[1]; 
				param[key] = decodeURIComponent(value);
			}
			return param;
		};

		return self;
	}();

	window.Tools = tools;
	$.open = tools.open;
	$.openByParam = tools.openByParam;

}());